package com.xindaoapp.happypet.ui;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.utils.Constants;

public class WebViewActivity extends BaseActivity {
    private WebView web;

    // private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initViews() {
        super.initViews();

        if (getIntent().hasExtra(Constants.KEY_TOP_BAR_TITLE)) {
            setTopBarTitle(getIntent().getStringExtra(Constants.KEY_TOP_BAR_TITLE));
        }

        // Here is all of our WebView Components
        web = (WebView) findViewById(R.id.webview);
        web.setBackgroundColor(Color.parseColor("#ffffff"));
        web.setWebViewClient(new myWebClient());
        // progressBar = (ProgressBar) findViewById(R.id.progressbar);
        web.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int progress) {
                // progressBar.setProgress(progress);
            }
        });
        WebSettings websettings = web.getSettings();
        // websettings.setBuiltInZoomControls(true);
        websettings.setUseWideViewPort(true);
        websettings.setJavaScriptEnabled(true);
        websettings.setAllowFileAccess(true);
        websettings.setDomStorageEnabled(true);
        websettings.setLoadWithOverviewMode(true);
        // websettings.setUserAgentString("Android");
        websettings.setAppCacheEnabled(true);
        websettings.setSavePassword(true);

        // Load the URL you want to load
        web.loadUrl(getIntent().getStringExtra(Constants.KEY_VIDEO_URL));
        // Also can be used like this
        // web.loadUrl("http://www.url.com"");
    }

    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                finish();
            }
        };
    }

    @Override
    protected int getTopBarLeftImageViewRes() {
        return R.drawable.top_bar_back;
    }

    @Override
    protected void initEvents() {
        super.initEvents();
    }

    public class myWebClient extends WebViewClient {

        @Override
        public void onPageStarted(final WebView view, final String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            // Show the progress bar when page starts loading
            // progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public boolean shouldOverrideUrlLoading(final WebView view, String url) {
            // Here we allow other links to be clicked and opened
            view.loadUrl(url);
            // Make sure the progress bar is visable
            // progressBar.setVisibility(View.VISIBLE);
            return true;
        }

        public void onProgressChanged(WebView view, int progress) {
            // Here is where the page is loading
            // progressBar.setProgress(progress);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description,
                String failingUrl) {
            // Here you can place code to execute when there is a error, like No
            // data or a bad URL
        };

        @Override
        public void onPageFinished(WebView view, final String url) {
            super.onPageFinished(view, url);

            // Here we disable the progress bar when the page finishs loading
            // progressBar.setVisibility(View.GONE);
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (web.canGoBack()) {
                web.goBack();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        web.removeAllViews();
        web.destroy();
    }
}
